package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.PermissionMapper;
import com.zhongyu.mapper.RolePermissionMapper;
import com.zhongyu.pojo.Permission;
import com.zhongyu.pojo.RolePermission;
import com.zhongyu.service.PermissionService;
import com.zhongyu.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import sun.reflect.generics.tree.Tree;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission> implements PermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    private List<TreeNode> searchByParentId(Integer parentId,Integer roleId){
       List<Integer> permIds=rolePermissionMapper.findPermIds(roleId);
        List<TreeNode> nodes=new ArrayList<>();
        QueryWrapper wrapper= Wrappers.<Permission>query();
     if (parentId==null) {
         wrapper.isNull("parent_id");
     }else {

         wrapper.eq("parent_id",parentId);
     }
        List<Permission> list= super.list(wrapper);

     for (Permission p:list){//把permission转为treenode
       TreeNode node=new TreeNode();
         node.setId(p.getId());
         node.setExpand(false);
         QueryWrapper queryWrapper=new QueryWrapper();
         queryWrapper.eq("parent_id",p.getId());
       Integer count=  this.baseMapper.selectCount(queryWrapper);//permission里默认包含一个baseMapper
         if (permIds.contains(p.getId())&&count==0){//包含有p.getId没有子节点
             node.setChecked(true);//勾选默认是false
         }
         /*node.setExtend(false);//默认是闭合状态*/
         /*  node.setChecked(false);*/
         node.setTitle(p.getName());
       nodes.add(node);
     }
     return nodes;
    }
    private List<Permission> getPermissions(Integer parentId){
        QueryWrapper wrapper= Wrappers.<Permission>query();
        if (parentId==null) {
            wrapper.isNull("parent_id");
        }else {

            wrapper.eq("parent_id",parentId);
        }
        wrapper.orderByAsc("sort");//升序
       return super.list(wrapper);
    }

    @Override
    public List<TreeNode> search4Tree(Integer id) {
    List<TreeNode> nodes01=this.searchByParentId(null,id);//1级栏目

    for (TreeNode node01:nodes01){
        List<TreeNode> nodes02=this.searchByParentId(node01.getId(),id);//2级栏目
        node01.setChildren(nodes02);

        for (TreeNode node02:nodes02){
         List<TreeNode> nodes03= this.searchByParentId(node02.getId(),id);//3级节点
          node02.setChildren(nodes03);
        }
    }
        return nodes01;
    }

    @Override
    public void grant(Integer roleId, Integer[] ids) {
     rolePermissionMapper.deleteById(roleId);
     if (ids!=null&&ids.length>0){
         for (Integer id:ids){
             RolePermission rolePermission=new RolePermission(roleId,id);
             rolePermissionMapper.insert(rolePermission);
         }
     }
    }
    // int count=0;
    @Override
    public List<String> searchPermCodes(Integer roleId) {
  //log.debug("*************统计searchPermCodes方法执行的次数****************"+(++count));
        return baseMapper.findPermCodes(roleId);
    }

    @Override
    public List<Permission> searchMenulist() {
        Subject subject= SecurityUtils.getSubject();
       List<Permission> menuList01=this.getPermissions(null);
      menuList01= menuList01.stream()
               .filter(p->subject.isPermitted(p.getPermCode()))
               .collect(Collectors.toList());//对集合进行过滤return=true放到集合中
       for (Permission p01:menuList01){
       List<Permission> menuList02=this.getPermissions(p01.getId());
           menuList02= menuList02.stream()
                   .filter(p->subject.isPermitted(p.getPermCode()))
                   .collect(Collectors.toList());//对集合进行过滤return=true放到集合中
       p01.getChildren().addAll(menuList02);
       }
        return menuList01;
    }
}
